<%@ page import="java.util.List" %>
<%@ page import="com.kgc.entity.News" %>
<%@ page import="com.kgc.util.PageSupport" %>

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
    String path = request.getContextPath();
    String basePath = request.getServerName() + ":" + request.getServerPort() + path + "/";
    String baseUrlPath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
    pageContext.setAttribute("baseUrlPath", baseUrlPath);
%>
<html>
<head>

    <title>Title</title>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/css/common.css">
    <script src="<%=request.getContextPath()%>/static/js/jquery-1.9.1.min.js"></script>
    <style>
        .admin-list > tbody > tr > td {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .goPointer {
            cursor: pointer;
        }
    </style>
</head>

<body>
<jsp:useBean id="newsService"
             class="com.kgc.service.impl.NewsServiceImpl"
></jsp:useBean>
<%
    /*List<News> newsList = newsService.queryNewsAll();
    //把newsList集合存入页面作用域
    pageContext.setAttribute("newsList",newsList);*/
    /*
    计算当前页码
    用户发起列表查询请求时，如果没有指明页码，默认为第一页
    如果传过来页码，就采用传过来的页码
     */
    int curPageNo=1;
    if(request.getParameter("curPageNo")!=null && !request.getParameter("curPageNo").equals("")){
        curPageNo=Integer.parseInt(request.getParameter("curPageNo"));
    }
    PageSupport<News> pageSupport = PageSupport.getInstance(curPageNo);
    newsService.queryPageNews(pageSupport);
    pageContext.setAttribute("page",pageSupport);
%>
<!--主体-->
<div class="main-content-right">
    <!--即时新闻-->
    <div class="main-text-box">
        <div class="main-text-box-tbg">
            <div class="main-text-box-bbg">
                <form name="searchForm" id="searchForm"
                      action="#" method="post">
                    <div>
                        新闻分类：
                        <select name="categoryId">
                            <option value="0">全部</option>
                            <option value='1'>国内</option>
                            <option value='2'>国际</option>
                            <option value='3'>娱乐</option>
                            <option value='4'>军事</option>
                            <option value='5'>财经</option>
                            <option value='6'>天气</option>
                        </select>
                        新闻标题<input type="text" name="title" id="title" value=''/>
                        <button type="submit" class="page-btn goPointer">GO</button>
                        <button type="button" onclick="javaScript:location='newsDetailCreate.jsp';"
                                class="page-btn goPointer">增加
                        </button>
                        <input type="hidden" name="pageIndex" value="1"/>
                    </div>
                </form>
                <table cellpadding="1" cellspacing="1" class="admin-list">
                    <thead>
                    <tr class="admin-list-head">
                        <th>新闻标题</th>
                        <th>作者</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach items="${page.datas}" var="news" varStatus="vs">
                        <%--<c:choose>
                            <c:when test="${vs.count%2==0}">
                                <tr class="admin-list-td-h2">
                            </c:when>
                            <c:otherwise><tr></c:otherwise>
                        </c:choose>--%>
                        <c:if test="vs.count%2==0">
                            <tr class="admin-list-td-h2">
                        </c:if>
                        <c:if test="vs.count%2!=0">
                            <tr>
                        </c:if>
                        <tr>
                            <td><a href="newsDetailView.jsp?id=${news.id}">${news.title}</a></td>
                            <td>${news.author}</td>
                            <td>
                                <fmt:formatDate value="${news.createDate}" pattern="yyyy-MM-dd HH:mm"></fmt:formatDate>
                            </td>
                            <td>
                                <a href="#">修改</a>
                                <a href="javaScript:if(confirm('您确认删除此条新闻吗？')){location='#'}">删除</a>
                            </td>
                        </tr>
                    </c:forEach>
                    <tr>
                        <td colspan="4">
                            共${page.total}条记录
                            &emsp;
                            ${page.curPageNo}/${page.pages}
                            &emsp;
                            <c:if test="${page.curPageNo>1}">
                                <a href="newsDetailList.jsp?curPageNo=1">首页</a>
                                <a href="newsDetailList.jsp?curPageNo=${page.curPageNo-1}">上一页</a>
                            </c:if>
                            &emsp;
                            <c:forEach var="curPageNo" begin="1" end="${page.pages}" step="1">
                                <c:if test="${curPageNo==page.curPageNo}">
                                    <a href="newsDetailList.jsp?curPageNo=${curPageNo}" style="color: #2b66c9;font-weight: bold">${curPageNo}</a>
                                </c:if>
                                <c:if test="${curPageNo!=page.curPageNo}">
                                    <a href="newsDetailList.jsp?curPageNo=${curPageNo}">${curPageNo}</a>
                                </c:if>
                                &emsp;
                            </c:forEach>
                            <c:if test="${page.curPageNo<page.pages}">
                                <a href="newsDetailList.jsp?curPageNo=${page.curPageNo+1}">下一页</a>
                                &emsp;
                                <a href="newsDetailList.jsp?curPageNo=${page.pages}">尾页</a>
                            </c:if>
                            &emsp;
                            跳转至
                            <input type="number" id="curPageNo"
                                   value="${page.curPageNo}" style="width:30px;">
                            页
                            <button onclick="jumpToPage()">Go</button>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<script>
    //跳转到指定页码数据的函数
    function jumpToPage(){
        var curPageNo = $("#curPageNo").val();
        if(curPageNo<1 || curPageNo>${page.pages}){
            alert("页码输入不合法！")
            return;
        }else {
            location="newsDetailList.jsp?curPageNo="+curPageNo;
        }
    }

</script>
</body>
</html>